"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dbcompatibility-whale
Case Name   : 函数public.decode()使用expr1和expr2进行比较
Create At   : 2023/12/26
Owner       : @zou_jialiang050
Description :
    1.函数public.decode()使用expr1和expr2进行比较
    2.函数public.decode错参
    3.函数public.decode无参
    4.函数public.decode少参
Expect      :
    1.成功
    2.失败
    3.失败
    4.失败
History     :
"""
import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Whale(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.common = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.dbname = 'db_whale_99'
    
    def test_decode(self):
        text = "---pre1:创建A兼容性数据库;expect:成功---"
        self.logger.info(text)
        pre_cmd1 = f"drop database if exists {self.dbname};" \
            f"create database {self.dbname} dbcompatibility='A';"
        pre_msg1 = self.sh_primy.execut_db_sql(pre_cmd1)
        self.logger.info(pre_msg1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, pre_msg1,
                      text + '执行失败')
        text = "---pre2:在A数据库中创建whale插件;expect:成功---"
        self.logger.info(text)
        pre_cmd2 = f"create extension whale;"
        pre_msg2 = self.sh_primy.execut_db_sql(pre_cmd2, dbname=self.dbname)
        self.logger.info(pre_msg2)
        self.assertIn(self.constant.extension_create_success, pre_msg2,
                      text + '执行失败')
        
        text = "---step1:返回参数列表中第一个非NULL的参数值;expect:成功---"
        self.logger.info(text)
        sql_cmd1_1 = f"select public.decode('c'::bpchar, 'a'::bpchar," \
            f"'postgres'::bpchar);"
        msg1_1 = self.sh_primy.execut_db_sql(sql_cmd1_1, dbname=self.dbname)
        self.logger.info(msg1_1)
        self.common.equal_sql_mdg(msg1_1, 'decode', '', '(1 row)', flag='1')

        sql_cmd1_2 = f" select public.decode('a'::bpchar,'a'::bpchar," \
            f"'postgres'::bpchar,'default value'::bpchar);"
        msg1_2 = self.sh_primy.execut_db_sql(sql_cmd1_2, dbname=self.dbname)
        self.logger.info(msg1_2)
        self.common.equal_sql_mdg(msg1_2, 'decode', 'postgres', '(1 row)',
                                  flag='1')

        sql_cmd1_3 = f"select public.decode('c','a'::bpchar," \
            f"'postgres'::bpchar,'default value'::bpchar);"
        msg1_3 = self.sh_primy.execut_db_sql(sql_cmd1_3, dbname=self.dbname)
        self.logger.info(msg1_3)
        self.common.equal_sql_mdg(msg1_3, 'decode', 'default value',
                                  '(1 row)', flag='1')

        sql_cmd1_4 = f"select public.decode('a'::bpchar,'a'::bpchar," \
            f"'postgres'::bpchar,'b'::bpchar,'database'::bpchar);"
        msg1_4 = self.sh_primy.execut_db_sql(sql_cmd1_4, dbname=self.dbname)
        self.logger.info(msg1_4)
        self.common.equal_sql_mdg(msg1_4, 'decode', 'postgres',
                                  '(1 row)', flag='1')

        sql_cmd1_5 = f" select public.decode('d'::bpchar,'a'::bpchar," \
            f"'postgres'::bpchar,'b'::bpchar,'database'::bpchar);"
        msg1_5 = self.sh_primy.execut_db_sql(sql_cmd1_5, dbname=self.dbname)
        self.logger.info(msg1_5)
        self.common.equal_sql_mdg(msg1_5, 'decode', '', '(1 row)', flag='1')

        sql_cmd1_6 = f"select public.decode('a'::bpchar,'a'::bpchar," \
            f"'postgres'::bpchar,'b'::bpchar,'database'::bpchar," \
            f"'default value'::bpchar);"
        msg1_6 = self.sh_primy.execut_db_sql(sql_cmd1_6, dbname=self.dbname)
        self.logger.info(msg1_6)
        self.common.equal_sql_mdg(msg1_6, 'decode', 'postgres',
                                  '(1 row)', flag='1')

        sql_cmd1_7 = f"select public.decode('d'::bpchar,'a'::bpchar," \
            f"'postgres'::bpchar,'b'::bpchar,'database'::bpchar," \
            f"'default value'::bpchar);"
        msg1_7 = self.sh_primy.execut_db_sql(sql_cmd1_7, dbname=self.dbname)
        self.logger.info(msg1_7)
        self.common.equal_sql_mdg(msg1_7, 'decode', 'default value',
                                  '(1 row)', flag='1')

        sql_cmd1_8 = f"select public.decode('a'::bpchar,'a'::bpchar," \
            f"'postgres'::bpchar,'b'::bpchar,'database'::bpchar,'c'::bpchar," \
            f"'system'::bpchar);"
        msg1_8 = self.sh_primy.execut_db_sql(sql_cmd1_8, dbname=self.dbname)
        self.logger.info(msg1_8)
        self.common.equal_sql_mdg(msg1_8, 'decode', 'postgres',
                                  '(1 row)', flag='1')

        sql_cmd1_9 = f"select public.decode('d'::bpchar,'a'::bpchar," \
            f"'postgres'::bpchar,'b'::bpchar,'database'::bpchar,'c'::bpchar," \
            f"'system'::bpchar);"
        msg1_9 = self.sh_primy.execut_db_sql(sql_cmd1_9, dbname=self.dbname)
        self.logger.info(msg1_9)
        self.common.equal_sql_mdg(msg1_9, 'decode', '', '(1 row)', flag='1')

        sql_cmd1_10 = f"select public.decode('a'::bpchar,'a'::bpchar," \
            f"'postgres'::bpchar,'b'::bpchar,'database'::bpchar,'c'::bpchar," \
            f"'system'::bpchar,'default value'::bpchar);"
        msg1_10 = self.sh_primy.execut_db_sql(sql_cmd1_10, dbname=self.dbname)
        self.logger.info(msg1_10)
        self.common.equal_sql_mdg(msg1_10, 'decode', 'postgres',
                                  '(1 row)', flag='1')

        sql_cmd1_11 = f"select public.decode('d'::bpchar,'a'::bpchar," \
            f"'postgres'::bpchar,'b'::bpchar,'database'::bpchar,'c'::bpchar," \
            f"'system'::bpchar,'default value'::bpchar);"
        msg1_11 = self.sh_primy.execut_db_sql(sql_cmd1_11, dbname=self.dbname)
        self.logger.info(msg1_11)
        self.common.equal_sql_mdg(msg1_11, 'decode', 'default value',
                                  '(1 row)', flag='1')

        sql_cmd1_12 = f"select public.decode(NULL,'a'::bpchar," \
            f"'postgres'::bpchar, NULL,'database'::bpchar);"
        msg1_12 = self.sh_primy.execut_db_sql(sql_cmd1_12, dbname=self.dbname)
        self.logger.info(msg1_12)
        self.common.equal_sql_mdg(msg1_12, 'decode', 'database',
                                  '(1 row)', flag='1')

        sql_cmd1_13 = f"select public.decode(NULL,'a'::bpchar," \
            f"'postgres'::bpchar,'b'::bpchar,'database'::bpchar);"
        msg1_13 = self.sh_primy.execut_db_sql(sql_cmd1_13, dbname=self.dbname)
        self.logger.info(msg1_13)
        self.common.equal_sql_mdg(msg1_13, 'decode', '', '(1 row)', flag='1')

        sql_cmd1_14 = f"select public.decode(NULL,'a'::bpchar," \
            f"'postgres'::bpchar, NULL,'database'::bpchar," \
            f"'default value'::bpchar);"
        msg1_14 = self.sh_primy.execut_db_sql(sql_cmd1_14, dbname=self.dbname)
        self.logger.info(msg1_14)
        self.common.equal_sql_mdg(msg1_14, 'decode', 'database',
                                  '(1 row)', flag='1')

        sql_cmd1_15 = f"select public.decode(NULL,'a'::bpchar," \
            f"'postgres'::bpchar,'b'::bpchar,'database'::bpchar," \
            f"'default value'::bpchar);"
        msg1_15 = self.sh_primy.execut_db_sql(sql_cmd1_15, dbname=self.dbname)
        self.logger.info(msg1_15)
        self.common.equal_sql_mdg(msg1_15, 'decode', 'default value',
                                  '(1 row)', flag='1')
        
        text = "---step2:函数public.decode参数为null;expect:失败---"
        self.logger.info(text)
        sql_cmd2 = f"select public.decode(null, null);"
        msg2 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.dbname)
        self.logger.info(msg2)
        self.assertIn('function public.decode(unknown, unknown) does '
                      'not exist', msg2, text + '执行失败')

        text = "---step3:函数public.decode无参;expect:失败---"
        self.logger.info(text)
        sql_cmd3 = f"select public.decode();"
        msg3 = self.sh_primy.execut_db_sql(sql_cmd3, dbname=self.dbname)
        self.logger.info(msg3)
        self.assertIn("function public.decode() does not exist",
                      msg3, text + '执行失败')

        text = "---step4:函数nanvl少参;expect:失败---"
        self.logger.info(text)
        sql_cmd4 = f"select public.decode('abc'::text);"
        msg4 = self.sh_primy.execut_db_sql(sql_cmd4, dbname=self.dbname)
        self.logger.info(msg4)
        self.assertIn("function public.decode(text) does not exist", msg4,
                      text + '执行失败')

    def tearDown(self):
        text = '----清理环境----'
        cl_cmd = f"drop database {self.dbname};"
        cl_msg = self.sh_primy.execut_db_sql(cl_cmd)
        self.logger.info(cl_msg)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, cl_msg,
                      text + '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
